package com.bilibili.bilibili.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.adapter.BangumiPLadapter;
import com.bilibili.bilibili.adapter.BangumiXQadapter;
import com.bilibili.bilibili.adapter.Bangumiadapter;
import com.bilibili.bilibili.bean.BangumiDetails;
import com.bilibili.bilibili.bean.BangumiXQ;
import com.bilibili.bilibili.bean.BangumiqlBean;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GsonUtil;
import com.bilibili.bilibili.utils.URLUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * �����������
 */

public class Activitybangumi extends AppCompatActivity {


    private ImageView bangumi_bg;
    /*
    *
    * */
    private ImageView bangumi_pic;
    private RecyclerView bangumi_recommend_recycler;
    private Bangumiadapter bangumiadapter;
    Gson gson = new Gson();
    private BangumiPLadapter bangumiPLadapter;
    private RecyclerView bangumi_selection_recycler;
    private BangumiXQadapter bangumiXQadapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitybangumi);

        initid();
        initdata();


    }

    //��ȡ����
    private void initdata() {
        //��������ѡ��
        HttpGetData.RequestGet(this, URLUtils.DRAMA_HEAD + URLUtils.BANGUMIXQ, "zxw", new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                try {
                    String json = new String(result.getBytes("ISO-8859-1"), "UTF-8");
                    initd();
                    BangumiXQ bangumiXQ = GsonUtil.parseJsonToObj(json, new TypeToken<BangumiXQ>() {
                    });
                    List<BangumiXQ.ResultEntity.EpisodesEntity> episodes = bangumiXQ.getResult().getEpisodes();
                    bangumiXQadapter.getdata(episodes);

                    Glide.with(Activitybangumi.this).load(bangumiXQ.getResult().getCover()).into(bangumi_pic);

                    //���ñ�����˹ģ��ͼƬ
//                    Glide.with(Activitybangumi.this)
//                            .load(bangumiXQ.getResult().getCover())
//                            .crossFade(1000)
//                            .bitmapTransform(new BlurTransformation(Activitybangumi.this, 23, 4)) // ��23��������ģ����(��0.0��25.0֮��)��Ĭ�ϡ�25";"4":ͼƬ���ű���,Ĭ�ϡ�1����
//                            .into(bangumi_bg);

                    Glide.with(Activitybangumi.this).load(bangumiXQ.getResult().getCover()).bitmapTransform(new BlurTransformation(Activitybangumi.this)).into(bangumi_bg);


                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });


    }

    private void initd() {
        //����ķ����Ƽ�
        HttpGetData.RequestGet(this, URLUtils.DRAMA_HEAD + URLUtils.BANGUMIDETAILS, "zxw", new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                try {
                    String json = new String(result.getBytes("ISO-8859-1"), "UTF-8");
                    BangumiDetails bangumiDetails = GsonUtil.parseJsonToObj(json, new TypeToken<BangumiDetails>() {
                    });
                    BangumiDetails.ResultEntity result1 = bangumiDetails.getResult();
                    List<BangumiDetails.ResultEntity> resultEntities = new ArrayList<BangumiDetails.ResultEntity>();
                    resultEntities.add(result1);
                    bangumiadapter.initdata(resultEntities);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
        //评论解析���
        try {
            InputStream open = getAssets().open("bangumi_ql.json");
            BangumiqlBean bangumiqlBean = gson.fromJson(new InputStreamReader(open), BangumiqlBean.class);
            List<BangumiqlBean.DataEntity.RepliesEntity> replies = bangumiqlBean.getData().getReplies();
            bangumiPLadapter.getData(replies);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //��ȡid
    private void initid() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        toolbar.setTitle("番剧详情");

        bangumi_bg = (ImageView) findViewById(R.id.bangumi_bg);
        bangumi_pic = (ImageView) findViewById(R.id.bangumi_pic);
        /*
        * ���
        * */
        bangumi_selection_recycler = (RecyclerView) findViewById(R.id.bangumi_selection_recycler);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);//���������RecycleView
        bangumi_selection_recycler.setLayoutManager(linearLayoutManager1);
        bangumiXQadapter = new BangumiXQadapter(this);
        bangumi_selection_recycler.setAdapter(bangumiXQadapter);


        //�����Ƽ��µ�Recycleview
        bangumi_recommend_recycler = (RecyclerView) findViewById(R.id.bangumi_recommend_recycler);
        bangumi_recommend_recycler.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        bangumiadapter = new Bangumiadapter(this);
        bangumi_recommend_recycler.setAdapter(bangumiadapter);

        //选集recycleview
        RecyclerView bangumi_comment_recycler = (RecyclerView) findViewById(R.id.bangumi_comment_recycler);
        bangumi_comment_recycler.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        bangumiPLadapter = new BangumiPLadapter(this);
        bangumi_comment_recycler.setAdapter(bangumiPLadapter);

    }

}
